package com.express.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;

/**
 * 阿里短信帮助类
 */
public class SmsAlidayu {

  public static Logger logger = LoggerFactory.getLogger(SmsAlidayu.class);

  private static String product = "Dysmsapi";
  private static String domain = "dysmsapi.aliyuncs.com";
//  private static String accessKeyId = "LTAIxki9VtNusECP";
  private static String accessKeyId = "LTAI4G396h46UGq4DXNpUVUz";
//  private static String accessKeySecret = "iOfHMr44iHwBhJ8RN29fQeVD1MXKYk";
  private static String accessKeySecret = "pAUbBWWVOGUc94Cd0jfxqmeRYAGA8h";

  public static SendSmsResponse sendSms(String phone, String freeSignName, String smsTemplateCode, Map<String, String> param)
          throws ClientException {
    System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
    System.setProperty("sun.net.client.defaultReadTimeout", "10000");

    IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
    DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
    IAcsClient acsClient = new DefaultAcsClient(profile);

    SendSmsRequest request = new SendSmsRequest();
    request.setPhoneNumbers(phone);
    request.setSignName(freeSignName);
    request.setTemplateCode(smsTemplateCode);
    String smsParam = JSONObject.toJSONString(param);
    logger.info("短信通知参数：smsParam:" + smsParam);
    request.setTemplateParam(smsParam);

    SendSmsResponse sendSmsResponse = (SendSmsResponse) acsClient.getAcsResponse(request);
    return sendSmsResponse;
  }

  public static CommonResponse sendSms2(String phone, String freeSignName, String smsTemplateCode, Map<String, String> param)
          throws ClientException {
    DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
    IAcsClient client = new DefaultAcsClient(profile);

    CommonRequest request = new CommonRequest();
    request.setMethod(MethodType.POST);
    request.setDomain("dysmsapi.aliyuncs.com");
    request.setVersion("2017-05-25");
    request.setAction("SendSms");
    request.putQueryParameter("PhoneNumbers", phone);
    logger.info("待发送号码" + phone + ";短信通知参数smsParam:" + JSONObject.toJSONString(param));
    request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
    request.putQueryParameter("SignName", freeSignName);
    request.putQueryParameter("TemplateCode", smsTemplateCode);
    try {
      CommonResponse response = client.getCommonResponse(request);
      logger.info("短信接口返回数据:" + response.getData());
      return response;
    } catch (ServerException e) {
      e.printStackTrace();
    } catch (ClientException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static void main(String[] args) {
    DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
    IAcsClient client = new DefaultAcsClient(profile);

    Map<String, Object> param = new HashMap();
    param.put("code", "100503");
    CommonRequest request = new CommonRequest();

    request.setMethod(MethodType.POST);
    request.setDomain("dysmsapi.aliyuncs.com");
    request.setVersion("2017-05-25");
    request.setAction("SendSms");
//    request.putQueryParameter("PhoneNumbers", "13660250055");
    request.putQueryParameter("PhoneNumbers", "13631370863");
    request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
    request.putQueryParameter("SignName", "广州莱比特");
    request.putQueryParameter("TemplateCode", "SMS_205350052");
//    request.putQueryParameter("TemplateCode", "SMS_139925163");
    try {
      CommonResponse response = client.getCommonResponse(request);
      System.out.println(response.getData());
    } catch (ServerException e) {
      e.printStackTrace();
    } catch (ClientException e) {
      e.printStackTrace();
    }
  }
}
